unit U_AltarAluguel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RXCtrls, StdCtrls, JvExStdCtrls, JvEdit, JvValidateEdit,
  Buttons, ExtCtrls, MMJPanel, DBXpress;

type
  TF_AltarAluguel = class(TForm)
    MMJPanel1: TMMJPanel;
    MMJPanel2: TMMJPanel;
    btn1: TSpeedButton;
    btn2: TSpeedButton;
    edt_Novo: TJvValidateEdit;
    RxLabel1: TRxLabel;
    lbl1: TRxLabel;
    edt_velho: TJvValidateEdit;
    RxLabel2: TRxLabel;
    edt_Taxa: TJvValidateEdit;
    JvValidateEdit1: TJvValidateEdit;
    procedure btn1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure edt_NovoChange(Sender: TObject);
    procedure btn2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  F_AltarAluguel: TF_AltarAluguel;

implementation

uses UDM, uFiltro_loca;

{$R *.dfm}

procedure TF_AltarAluguel.btn1Click(Sender: TObject);
var
  texto : string;
  TD: TTransactionDesc;
begin
  TD.TransactionID := 1;
  TD.IsolationLevel := xilREADCOMMITTED;
  dm.scSge.StartTransaction(TD);
  // Altera Aluguel
  try
    DecimalSeparator := '.';
    texto := 'update LANCAMENTOS SET VALOR = ' + FloatToStr(edt_Novo.Value);
    texto := texto + ' where COD_IMALU = ' + IntToStr(fFiltro_loca.cdsImoveisCOD_IMALU.AsInteger) +
                     ' and  COD_PROPRIETARIO = ' + IntToStr(fFiltro_loca.cdsImoveisCOD_PROPRIETARIO.AsInteger) +
                     ' and  COD_INQUILINO = ' + IntToStr(fFiltro_loca.cdsImoveisCOD_INQUILINO.AsInteger) +
                     ' and  MES >= ' + IntToStr(mes) +
                     ' and  ANO >= ' + IntToStr(ano) +
                     ' and  ATIVO = ' + QuotedStr('S') +
                     ' and  COD_CONTAS = ' + IntToStr(1);
    DM.scSge.ExecuteDirect(texto);
    dm.scSge.Commit(TD);
  except
     on E : Exception do
     begin
       ShowMessage('Classe: ' + e.ClassName + chr(13) + 'Mensagem: ' + e.Message);
       DM.scSge.Rollback(TD); //on failure, undo the changes
     end;
  end;
  dm.scSge.StartTransaction(TD);
  // Altera REPASSE
  try
    DecimalSeparator := '.';
    texto := 'update LANCAMENTOS SET VALOR = ' + FloatToStr(edt_Novo.Value);
    texto := texto + ' where COD_IMALU = ' + IntToStr(fFiltro_loca.cdsImoveisCOD_IMALU.AsInteger) +
                     ' and  COD_PROPRIETARIO = ' + IntToStr(fFiltro_loca.cdsImoveisCOD_PROPRIETARIO.AsInteger) +
                     ' and  COD_INQUILINO = ' + IntToStr(fFiltro_loca.cdsImoveisCOD_INQUILINO.AsInteger) +
                     ' and  MES >= ' + IntToStr(mes) +
                     ' and  ANO >= ' + IntToStr(ano) +
                     ' and  ATIVO = ' + QuotedStr('S') +
                     ' and  COD_CONTAS = ' + IntToStr(2);
    DM.scSge.ExecuteDirect(texto);
    dm.scSge.Commit(TD);
  except
     on E : Exception do
     begin
       ShowMessage('Classe: ' + e.ClassName + chr(13) + 'Mensagem: ' + e.Message);
       DM.scSge.Rollback(TD); //on failure, undo the changes
     end;
  end;
  dm.scSge.StartTransaction(TD);
  // Taxa ADM
  try
    DecimalSeparator := '.';
    texto := 'update LANCAMENTOS SET VALOR = ' + FloatToStr(JvValidateEdit1.Value);
    texto := texto + ' where COD_IMALU = ' + IntToStr(fFiltro_loca.cdsImoveisCOD_IMALU.AsInteger) +
                     ' and  COD_PROPRIETARIO = ' + IntToStr(fFiltro_loca.cdsImoveisCOD_PROPRIETARIO.AsInteger) +
                     ' and  COD_INQUILINO = ' + IntToStr(fFiltro_loca.cdsImoveisCOD_INQUILINO.AsInteger) +
                     ' and  MES >= ' + IntToStr(mes) +
                     ' and  ANO >= ' + IntToStr(ano) +
                     ' and  ATIVO = ' + QuotedStr('S') +
                     ' and  COD_CONTAS = ' + IntToStr(14);
    DM.scSge.ExecuteDirect(texto);
    dm.scSge.Commit(TD);
  except
     on E : Exception do
     begin
       ShowMessage('Classe: ' + e.ClassName + chr(13) + 'Mensagem: ' + e.Message);
       DM.scSge.Rollback(TD); //on failure, undo the changes
     end;
  end;

 dm.scSge.StartTransaction(TD);
  // libera imovel
  try
    DecimalSeparator := '.';
    texto := 'UPDATE IMOVEL_ALUGUEL SET VALOR_ALUGUEL = ' + FloatToStr(edt_Novo.Value);
   // texto := texto + ' , TAXA_ADM = ' + FloatToStr(JvValidateEdit1.Value) +
    texto := texto + ' where COD_IMALU = ' + IntToStr(fFiltro_loca.cdsImoveisCOD_IMALU.AsInteger) +
                     ' and  COD_PROPRIETARIO = ' + IntToStr(fFiltro_loca.cdsImoveisCOD_PROPRIETARIO.AsInteger) +
                     ' and  COD_INQUILINO = ' + IntToStr(fFiltro_loca.cdsImoveisCOD_INQUILINO.AsInteger) ;

    DM.scSge.ExecuteDirect(texto);
    dm.scSge.Commit(TD);
  except
     on E : Exception do
     begin
       ShowMessage('Classe: ' + e.ClassName + chr(13) + 'Mensagem: ' + e.Message);
       DM.scSge.Rollback(TD); //on failure, undo the changes
     end;
  end;


end;

procedure TF_AltarAluguel.FormShow(Sender: TObject);
begin
   edt_Taxa.Value  := fFiltro_loca.cdsImoveisPORCENTUAL.Value;
   edt_velho.Value := fFiltro_loca.cdsImoveisVALOR_ALUGUEL.Value;
end;

procedure TF_AltarAluguel.edt_NovoChange(Sender: TObject);
begin
  JvValidateEdit1.Value := edt_Novo.Value *  (edt_Taxa.Value / 100);
end;

procedure TF_AltarAluguel.btn2Click(Sender: TObject);
begin
    close;
end;

end.
